﻿using Anmlz.Model.Entities;
using LumenWorks.Framework.IO.Csv;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Anmlz.Services
{
    using Anmlz.Services.Contracts;

    class CountryImportItem
    {
        public string country_id { get; set; }
        public string title_ru { get; set; }
        public string title_en { get; set; }
    }
    class CityImportItem
    {
        public string city_id { get; set; }
        public string country_id { get; set; }
        public string important { get; set; }
        public string title_ru { get; set; }
        public string title_en { get; set; }
    }
    public class GeoParser : IGeoParser
    {
        public List<Country> ParseCountries(string fileUrl)
        {
            string[] lines = File.ReadAllLines(fileUrl);
            var cols = lines.First().Split(';');
            return lines.Skip(1).ToList().Select(line =>
            {
                CountryImportItem item = new CountryImportItem();
                var values = line.Split(';');
                for (int i = 0; i < cols.Count(); i++)
                {
                    var property = item.GetType().GetProperty(cols[i].Trim());
                    if (property != null)
                    {
                        property.SetValue(item, values[i], null);
                    }
                }
                return item;
            }).ToList().Select(importedItem => new Country()
            {
                Id = Convert.ToInt32(importedItem.country_id.Replace("\"", "")),
                NameRu = importedItem.title_ru.Replace("\"", ""),
                NameEn = importedItem.title_en.Replace("\"", "")
            }).ToList();
        }

        public List<City> ParseCities(string fileUrl)
        {
            List<CityImportItem> items = new List<CityImportItem>();
            using (CsvReader csv =
          new CsvReader(new StreamReader(fileUrl), true, ';'))
            {
                int fieldCount = csv.FieldCount;

                string[] headers = csv.GetFieldHeaders();
                while (csv.ReadNextRecord())
                {
                    CityImportItem item = new CityImportItem();

                    for (int i = 0; i < fieldCount; i++)
                    {
                        var property = item.GetType().GetProperty(headers[i].Trim());
                        if (property != null)
                        {
                            property.SetValue(item, csv[i], null);
                        }
                    }

                    items.Add(item);
                }
            }

            return items.Where(i => i.important == "t").Select(importedItem => new City()
             {
                 Id = Convert.ToInt32(importedItem.city_id.Replace("\"", "")),
                 CountryId = Convert.ToInt32(importedItem.country_id.Replace("\"", "")),
                 NameRu = importedItem.title_ru.Replace("\"", ""),
                 NameEn = importedItem.title_en.Replace("\"", "")
             }).ToList();
        }
    }
}
